`
https://leetcode.cn/problems/path-with-maximum-gold/
`

/**
 * @param {number[][]} grid
 * @return {number}
 */
var getMaximumGold = function (grid) {
  const m = grid.length, n = grid[0].length
  // 找到所有起点位置
  const starts = []
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (grid[i][j] !== 0) starts.push([i, j])
    }
  }

  const visited = Array.from({ length: m }, () => new Array(n).fill(false))
  let res = 0

  function dfs(i, j, sum) {
    // 边界和无矿
    if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] === 0) {
      return
    }

    // 被访问过就不需要访问了
    if (visited[i][j]) {
      return
    }

    const curSum = sum + grid[i][j]
    res = Math.max(res, curSum)
    visited[i][j] = true
    dfs(i + 1, j, curSum)
    dfs(i - 1, j, curSum)
    dfs(i, j + 1, curSum)
    dfs(i, j - 1, curSum)
    visited[i][j] = false
  }

  for (const start of starts) {
    const i = start[0], j = start[1]
    dfs(i, j, 0)
  }

  return res
};